/**
 * 
 */
package com.kexinde.wjifen.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kexinde.wjifen.model.Activity;
import com.kexinde.wjifen.model.Shop;
import com.kexinde.wjifen.service.IActivityManager;
import com.kexinde.wjifen.util.CreateUploadPath;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author vincent
 * 
 */
@Component("Activity")
@Scope("prototype")
public class ActivityAction extends ActionSupport implements
		ServletRequestAware, ServletContextAware, RequestAware {

	private IActivityManager activityManager;
	private Activity activity;
	private Shop shop;
	private File activityUpload; // 上传的文件
	private String activityUploadContentType; // 文件的内容类型
	private ServletContext context;
	private ServletRequest req;
	private Map<String, Object> request;

	public String showAll() { // 显示某店铺的所有优惠信息
		List<Activity> activities = this.activityManager.showAll(shop.getId());
		request.put("activities", activities);
		return SUCCESS;
	}

	public String showOne() { // 显示某店铺的某一优惠信息
		activity = this.activityManager.showOne(shop.getId(), activity.getId());
		request.put("activity", activity);
		return SUCCESS;
	}

	public String add() { // 添加优惠信息
		CreateUploadPath cup = new CreateUploadPath(activityUpload,
				activityUploadContentType, context, req,1);
		try {
			activity.setPicturePath(cup.execute());
		} catch (Exception e) {
			e.printStackTrace();
		}
		// System.out.println(activity.getName());
		// System.out.println(activity.getPicturePath());
		// System.out.println(activity.toString());
		// return SUCCESS;
		 if (this.activityManager.add(activity))
		 return SUCCESS;
		 else
		 return ERROR;
	}

	public String modify() { // 修改优惠信息
		CreateUploadPath cup = new CreateUploadPath(activityUpload,
				activityUploadContentType, context, req,1);
		try {
			activity.setPicturePath(cup.execute());
		} catch (Exception e) {
			e.printStackTrace();
		}
		// System.out.println(activity.getName());
		// System.out.println(activity.getPicturePath());
		// System.out.println(activity.toString());
		// return SUCCESS;
		 if (this.activityManager.modify(activity))
		 return SUCCESS;
		 else
		 return ERROR;
	}

	public String delete() { // 删除优惠信息
		this.activityManager.delete(activity);
		return SUCCESS;
	}

	@Override
	public void setServletContext(ServletContext context) {
		// TODO Auto-generated method stub
		this.context = context;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		// TODO Auto-generated method stub
		this.req = req;
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public File getActivityUpload() {
		return activityUpload;
	}

	public void setActivityUpload(File activityUpload) {
		this.activityUpload = activityUpload;
	}

	public IActivityManager getActivityManager() {
		return activityManager;
	}

	@Resource
	public void setActivityManager(IActivityManager activityManager) {
		this.activityManager = activityManager;
	}

	public String getActivityUploadContentType() {
		return activityUploadContentType;
	}

	public void setActivityUploadContentType(String activityUploadContentType) {
		this.activityUploadContentType = activityUploadContentType;
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		// TODO Auto-generated method stub
		this.request = request;
	}
}
